#include "Ctrl.h"


#if FlyweightPattern_

#include <unordered_map>
using namespace std::tr1;

// 用户
class User
{
private:
	string m_name;
public:
	User(string _name):m_name(_name){}
	string GetName() {
		return m_name;
	}
};

// 抽象
class WebSite
{
public :
	virtual void Use(User* _pUser) = 0;
};

// 具体
class ConcreteWebSite : public WebSite
{
private:
	string m_name = "";
public:
	ConcreteWebSite(string _name):m_name(_name){}
	void Use(User* _pUser) override {
		cout << "网站分类：" << m_name << " 用户：" << _pUser->GetName() << endl;
	}
};

// 工厂类
class WebSiteFactory
{
private:
	unordered_map<string,ConcreteWebSite*>* m_pFlyweights = new unordered_map<string, ConcreteWebSite*>;
public:
	// 获得网站分类
	WebSite* GetWebSiteCategory(string _key) {
		if (m_pFlyweights->find(_key) == m_pFlyweights->end())
			m_pFlyweights->insert(make_pair(_key, new ConcreteWebSite(_key)));
		return (WebSite*)m_pFlyweights->at(_key);
	}
	// 获得网站分类总数
	size_t GetWebSiteCount() const{
		return m_pFlyweights->size();
	}
};






int main() {
	WebSiteFactory* pWSF = new WebSiteFactory();

	WebSite* pWS = pWSF->GetWebSiteCategory("产品展示");
	pWS->Use(new User("小菜"));

	WebSite* pWS2 = pWSF->GetWebSiteCategory("产品展示");
	pWS2->Use(new User("大鸟"));

	WebSite* pWS3 = pWSF->GetWebSiteCategory("产品展示");
	pWS3->Use(new User("娇娇"));

	WebSite* pWS11 = pWSF->GetWebSiteCategory("博客");
	pWS11->Use(new User("老顽童"));

	WebSite* pWS12 = pWSF->GetWebSiteCategory("博客");
	pWS12->Use(new User("桃谷六仙"));

	WebSite* pWS13 = pWSF->GetWebSiteCategory("博客");
	pWS13->Use(new User("南海饿神"));

	cout << "得到网站分类总数为：" << pWSF->GetWebSiteCount() << endl;

}




#endif